#ifndef _MEMORYPOOL_HPP_
#define _MEMORYPOOL_HPP_

#include "jhdefs.hpp"
#include "event/mutex.hpp"
using JHCPP::event::CMutex;
#include <vector>

NET_NAMESPACE_BEGIN

class CMemoryPool
{
public:
	CMemoryPool(std::size_t blockSize, int preAlloc = 0, int maxAlloc = 0);
	~CMemoryPool();

	void* get();
	void release(void* ptr);
	std::size_t blockSize() const;
	int allocated() const;
	int available() const;

private:
	CMemoryPool();
	CMemoryPool(const CMemoryPool&);
	CMemoryPool& operator = (const CMemoryPool&);

	enum
	{
		BLOCK_RESERVE = 128
	};

	typedef std::vector<char*> BlockVec;

	std::size_t m_blockSize;
	int         m_maxAlloc;
	int         m_allocated;
	BlockVec    m_blocks;
	CMutex		m_mutex;
};

NET_NAMESPACE_END

#endif
